package net.office.utils;

import net.office.bean.StaffInfo;
import net.office.bean.WorkTable;

import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class RotaBean {
    static Calendar cal = Calendar.getInstance();
    public static final int currentYear = cal.get(Calendar.YEAR);//当前是哪一年
    public static final int currentMonth = cal.get(Calendar.MONTH )+1;//当前是哪个月，注意这里的月是从0开始计数的，month+1即为当前月
    public static final int currentDay = cal.get(Calendar.DATE);

    String calendar = null;
    private int year;
    private int month;

    private List<StaffInfo> staffInfoList;

    public List<StaffInfo> getStaffInfoList() {
        return staffInfoList;
    }
    public void setStaffInfoList(List<StaffInfo> staffInfoList) {
        this.staffInfoList = staffInfoList;
    }

    public void setYear(int year)
    {
        this.year = year;
    }
    public int getYear()
    {
        if(year==0){
            year=currentYear;
        }
        return year;
    }
    public void setMonth(int month)
    {
        this.month = month;
    }
    public int getMonth()
    {
        if(month==0){
            month=currentMonth;
        }
        return month;
    }

    public String getCalendar() {
        StringBuffer buffer = new StringBuffer();
        Calendar rili = Calendar.getInstance();
        if (year == 0 || month == 0) {
            year = currentYear;
            month = currentMonth;
        }
        rili.set(year, month - 1, 1);//將日历翻到year年month月1日，注意0表示一月，以此类推11表示12月
        int startDay = rili.get(Calendar.DAY_OF_WEEK) - 1;//获取1日是星期几(get方法返回的值是1表示星期日，返回的值是7表示星期六)
        int day = 0;
        if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) {
            day = 31;
        }
        if (month == 4 || month == 6 || month == 9 || month == 11) {
            day = 30;
        }
        if (month == 2) {
            if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) {
                day = 29;
            } else {
                day = 28;
            }
        }

        int flag = 0;
        if(month == currentMonth){
            flag = 1;
        }

        //用表格表示数組
        buffer.append("<table class=\"table table-bordered\" id=\"rota-table\">");
        buffer.append("<thead><tr>");
        String weekday[] = {"周日", "周一", "周二", "周三", "周四", "周五", "周六"};
        buffer.append("<td class=\"color-blue\">"+"工号"+"</td>"+"<td class=\"color-blue\">"+"姓名"+"</td>");
        for(int k=1; k<=day; k++){
            if(startDay+k-1 < 7){
                if(k==currentDay && flag==1){
                    buffer.append("<td class=\"color-blue\">");
                }else{
                    buffer.append("<td class=\"color-dark\">");
                }
                buffer.append(weekday[startDay+k-1]+k+"</td>");
            }else{
                if(k==currentDay && flag==1){
                    buffer.append("<td class=\"color-blue\">");
                }else{
                    buffer.append("<td class=\"color-dark\">");
                }
                buffer.append(weekday[(startDay+k-1)%7]+k+"</td>");
            }
        }
        buffer.append("</tr></thead>");
        buffer.append("<tbody>");
        for(int m=0; m<staffInfoList.size(); m++) {

            //记录当天是否需要值班
            int day_count = day + 1;
            int[] need_work = new int[day_count];
            for(int n=0; n<staffInfoList.get(m).getWorkTableList().size(); n++){
                WorkTable work = staffInfoList.get(m).getWorkTableList().get(n);
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(work.getWorkDate());
                int work_date = calendar.get(Calendar.DATE);
                need_work[work_date] = 1;
            }

            //记录当天是否值班
            int[] is_work = new int[day_count];
            for(int n=0; n<staffInfoList.get(m).getWorkTableList().size(); n++){

                WorkTable work = staffInfoList.get(m).getWorkTableList().get(n);
                if("1".equals(work.getIsWork())){

                    Calendar calendar = Calendar.getInstance();
                    calendar.setTime(work.getWorkDate());
                    int work_date = calendar.get(Calendar.DATE);
                    is_work[work_date] = 1;
                }
            }

            buffer.append("<tr>");
            for (int k = 1; k <= day+2; k++) {
                if (k > 2 && need_work[k-2] == 1 && is_work[k-2] == 1) {
                    buffer.append("<td class=\"yellow-bg color-white\">" + "值班" + "</td>");
                } else if (k > 2 && need_work[k-2] == 1 && is_work[k-2] == 0){
                    buffer.append("<td class=\"color-yellow\">" + "值班" + "</td>");
                } else if (k == 1) {
                   buffer.append("<td class=\"color-yellow\">" + staffInfoList.get(m).getStaffNo() + "</td>");
                } else if (k == 2) {
                    buffer.append("<td class=\"color-yellow\">" + staffInfoList.get(m).getStaffName() + "</td>");
                } else {
                    buffer.append("<td>" + "&nbsp;" + "</td>");
                }
            }
            buffer.append("</tr>");
        }
        buffer.append("</tbody></table>");
        calendar = new String(buffer);
        return calendar;
    }

    public  String getStringDate(Date dateDate) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateString = formatter.format(dateDate);
        return dateString;
    }
    public Date strToDateLong(String strDate) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        ParsePosition pos = new ParsePosition(0);
        Date strtodate = formatter.parse(strDate, pos);
        return strtodate;
    }
    public String dateToStr(Date dateDate) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String dateString = formatter.format(dateDate);
        return dateString;
    }
    public Date strToDate(String strDate) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        ParsePosition pos = new ParsePosition(0);
        Date strtodate = formatter.parse(strDate, pos);
        return strtodate;
    }
    private boolean isSameDate(Date date1, Date date2) {
        Calendar cal1 = Calendar.getInstance();
        cal1.setTime(date1);

        Calendar cal2 = Calendar.getInstance();
        cal2.setTime(date2);

        boolean isSameYear = cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR);
        boolean isSameMonth = isSameYear && cal1.get(Calendar.MONTH) == cal2.get(Calendar.MONTH);
        boolean isSameDate = isSameMonth && cal1.get(Calendar.DAY_OF_MONTH) == cal2.get(Calendar.DAY_OF_MONTH);

        return isSameDate;
    }
}
